import mitt from 'mitt';
const emitter = mitt();

// 为了兼容 Vue 2 的 $on/$off API，包装成类似 Vue 的事件总线
const eventBus = {
  $on: (event, handler) => {
    emitter.on(event, handler);
  },
  $off: (event, handler) => {
    if (handler) {
      emitter.off(event, handler);
    } else {
      emitter.all.delete(event);
    }
  },
  $emit: (event, ...args) => {
    emitter.emit(event, ...args);
  },
  // 同时支持 mitt 的原生 API
  on: emitter.on.bind(emitter),
  off: emitter.off.bind(emitter),
  emit: emitter.emit.bind(emitter),
};

export default eventBus;
